﻿Namespace BoAddonBiz_Authorisation
    Public Class B1AuthorisationServer
        Private WithEvents SBOAddon As SAPBoAddon.B1AddonBase.AddonBase
        Private Shared _B1Authorisation As B1AuthorisationServer

        Public Shared Function CreateInstance() As B1AuthorisationServer
            If _B1Authorisation Is Nothing Then
                _B1Authorisation = New B1AuthorisationServer
                Return _B1Authorisation
            Else
                Return _B1Authorisation
            End If
        End Function

        Private Sub New()
            SBOAddon = SAPBoAddon.B1Addon.B1Addon.SBOAddOn
        End Sub


        Private Sub SBOAddon_AddonEvent(ByVal pVal As B1AddonBase.AddonEvent, ByRef BubbleEvent As Boolean) Handles SBOAddon.AddonEvent
            Try
                If pVal.BeforeEvent Then
                    If pVal.BaseEventType = B1AddonBase.EnumBaseEventType.et_FormDataSave Then
                        BubbleEvent = FormDataAuthorisation(pVal.FormType)
                    End If
                    If pVal.BaseEventType = B1AddonBase.EnumBaseEventType.et_MenuAddToB1 Then
                        BubbleEvent = MenuAuthorisation.Apply_Authorisation_Menu(pVal.MenuUID)
                    End If
                Else
                    If pVal.BaseEventType = B1AddonBase.EnumBaseEventType.et_FormLoad Then
                        If pVal.ActionSucess Then
                            InitializeFormAuthorisation(pVal.FormUID, pVal.FormType)
                        End If
                    End If

                End If
            Catch ex As Exception
                SBOAddon.StatusBarMessage(ex.Message & " by B1AuthorisationServer.", SAPbouiCOM.BoStatusBarMessageType.smt_Error)
            End Try

        End Sub

        ''' <summary>
        ''' 权限应用
        ''' 系统加载窗体后
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub InitializeFormAuthorisation(ByVal formUID As String, ByVal FormType As String)
            Dim oform As SAPbouiCOM.Form
            Dim Author As SAPBoAddon.BoAddonBiz_Authorisation.FormAuthorisation
            Try
                Try
                    oform = SBOAddon.SBOApp.Forms.Item(formUID)
                Catch ex As Exception
                    Throw New Exception("form [%0] not exists.".Replace("%0", formUID))
                End Try
                Author = SAPBoAddon.BoAddonBiz_Authorisation.FormAuthorisation.getFormAuthorisation(FormType)
                '控件权限——控件不能显示
                B1Assistant.Item.SetVisible(oform, Author.ItemAuthorisation_None, False)
                '权限设置——不可编辑的控件
                B1Assistant.Item.SetEnabled(oform, Author.ItemAuthorisation_ReadOnly, False)
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private Function FormDataAuthorisation(ByVal FormType As String)
            Dim Author As SAPBoAddon.BoAddonBiz_Authorisation.FormAuthorisation
            Try
                Author = SAPBoAddon.BoAddonBiz_Authorisation.FormAuthorisation.getFormAuthorisation(FormType)
                If Author.FormAuthorisationType = auAuthorisationType.READ Then
                    Return False
                End If
                Return True
            Catch ex As Exception
                Return True
            End Try
        End Function

        'Private Sub SBOAddon_B1FormDataEvent(ByVal BusinessObjectInfo As B1AddonBase.B1BusinessObjectInfo, ByRef BubbleEvent As Boolean) Handles SBOAddon.B1FormDataEvent

        '    If BusinessObjectInfo.BeforeAction Then

        '    Else
        '        If BusinessObjectInfo.ActionSuccess Then
        '            If BusinessObjectInfo.EventType = SAPbouiCOM.BoEventTypes.et_FORM_DATA_ADD And BusinessObjectInfo.EventType = SAPbouiCOM.BoEventTypes.et_FORM_DATA_LOAD Then

        '            End If
        '        End If
        '    End If

        'End Sub

        'Private Sub SBOAddon_B1ItemEvent(ByVal FormUID As String, ByVal pVal As B1AddonBase.B1ItemEvent, ByRef BubbleEvent As Boolean) Handles SBOAddon.B1ItemEvent

        '    If pVal.BeforeAction Then

        '    Else
        '        If pVal.ActionSuccess Then
        '            If pVal.ItemUID = "1" And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
        '                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_OK_MODE Then
        '                    InitializeFormAuthorisation(pVal.FormUID, pVal.FormTypeEx)
        '                End If
        '            End If
        '        End If
        '    End If
        'End Sub

        'Private Sub SBOAddon_B1MenuEvent(ByVal pVal As B1AddonBase.B1MenuEvent, ByRef BubbleEvent As Boolean) Handles SBOAddon.B1MenuEvent
        '    If pVal.BeforeAction Then
        '    Else
        '        If pVal.MenuUID = B1AddonBase.B1MenuUIDs.sys_NextRecord Or pVal.MenuUID = B1AddonBase.B1MenuUIDs.sys_PreviousRecord Or _
        '           pVal.MenuUID = B1AddonBase.B1MenuUIDs.sys_FirstRecord Or pVal.MenuUID = B1AddonBase.B1MenuUIDs.sys_LastRecord Then
        '            Dim oform As SAPbouiCOM.Form
        '            oform = GetActiveForm()
        '            If oform Is Nothing Then
        '            Else
        '                InitializeFormAuthorisation(oform.UniqueID, oform.TypeEx)
        '            End If
        '        End If
        '    End If
        'End Sub

        Private Function GetActiveForm() As SAPbouiCOM.Form
            Try
                Return SBOAddon.SBOApp.Forms.ActiveForm
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
    End Class
End Namespace

